<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.14.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>DM-CtrlH7-BF-DevProgram: 舵机的使用</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="50x5.png"/></td>
  <td id="projectalign">
   <div id="projectname">DM-CtrlH7-BF-DevProgram<span id="projectnumber">&#160;beta 0.1</span>
   </div>
   <div id="projectbrief">C.ONE Studio Damiao Development Board Framework</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.14.0 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search/",'.html');
</script>
<script type="text/javascript">
$(function() { codefold.init(); });
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search',true);
  $(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(function(){initNavTree('md__c_1_2_users_2_a_s_u_s_2_desktop_2dm-ctrl_h7-balance-9025test_2modules_2motor_2servo__motor_2servo__motor.html','',''); });
</script>
<div id="container">
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">舵机的使用 </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="autotoc_md80"></a></p>
<p align="left"></p>
<p><a href="#" onclick="location.href='mai'+'lto:'+'pan'+'ru'+'i@h'+'nu'+'.ed'+'u.'+'cn'; return false;">panru<span class="obfuscator">.nosp@m.</span>i@hn<span class="obfuscator">.nosp@m.</span>u.edu<span class="obfuscator">.nosp@m.</span>.cn</a></p>
<blockquote class="doxtable">
<p>todo: 由于新增了bsp_pwm的支持,舵机模块需要部分重构 </p>
</blockquote>
<h2 class="doxsection"><a class="anchor" id="autotoc_md81"></a>
舵机基础知识</h2>
<p>已最常见的SG90舵机为例，SG90舵机要求工作在频率为50HZ——周期为20ms的PWM波，且对应信号的高低电平在0.5ms - 2.5ms之间，对应的舵机转动角度如下表所示（当然也可以按照这个线性的对应关系去达到转动自己想要的角度，如想要转动60°，则高电平脉宽为大概为1.2ms，具体能不能转到特定的角度还和舵机的精度有关）</p>
<p>&gt;0.5ms----------&mdash;0度； 2.5% &gt;1.0ms---------&mdash;45度； 5.0% &gt;1.5ms---------&mdash;90度； 7.5% &gt;2.0ms--------&mdash;135度； 10.0% &gt;2.5ms--------&mdash;180度； 12.5%</p>
<p>根据<span class="tt">&lt;font color=black size=3&gt;Tout = (PSC+1)* (ARR+1)/Tclk&lt;/font&gt;</span>公式 则我们需要产生50Hz的PWM波，则预分频的系数为 Prescaler = 168-1，自动重装载值 Counter Period = 20000-1，此时定时器产生的频率为 168Mhz/168/20000 = 50Hz。 当然这个值也可以自己设置，只要满足产生的频率为50Hz即可. <span class="tt"><a class="el" href="group___t_i_m___exported___macros.html#ga300d0c9624c3b072d3afeb7cef639b66" title="Set the TIM Capture Compare Register value on runtime without calling another time ConfigChannel func...">__HAL_TIM_SET_COMPARE(htim, Channel, compare_value)</a>;</span> 这是设置占空比的函数 eg：当初始占空比为1200/20000则为6，根据20*6%=1.2ms （1.2-0.5）/(2.5-0.5)*180=63° 故舵机会转动63° 为了方便通过上述eg我们将所需要的角度与PWM计数值对应关系封装成函数。需要在初始化的時候输入我们所需要的角度和相关定时器参数即可。这样我们就可以设置SG90为参数范围内(0~180°)任意度数。</p>
<hr  />
<p> &lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<h1 class="doxsection"><a class="anchor" id="autotoc_md83"></a>
如何注册一个舵机实例</h1>
<p>!!!</p>
<p><b>注意！由于舵机为开环控制，无论选择舵机为何种类型，舵机都能够正常运行，但是运行的角度可能会与设定不同，请务必正确选择舵机型号！且最多添加7个舵机！</b> 我们可以像这样注册一个舵机实例 </p><div class="fragment"><div class="line"><span class="keyword">static</span> <a class="code hl_struct" href="struct_servo_instance.html">ServoInstance</a> *leftservomoto;</div>
<div class="line"><span class="comment">//初始化参数</span></div>
<div class="line"><a class="code hl_struct" href="struct_servo___init___config__s.html">Servo_Init_Config_s</a> config={</div>
<div class="line">    <span class="comment">//舵机安装选择的定时器及通道</span></div>
<div class="line">    <span class="comment">//C板有常用的7路PWM输出:TIM1-1,2,3,4 TIM8-1,2,3</span></div>
<div class="line">    .htim=&amp;htim1,</div>
<div class="line">    .Channel=<a class="code hl_define" href="group___t_i_m___channel.html#ga6b1541e4a49d62610899e24bf23f4879">TIM_CHANNEL_1</a>,</div>
<div class="line">    <span class="comment">//舵机的初始化模式和类型</span></div>
<div class="line">    .Servo_Angle_Type=Start_mode,</div>
<div class="line">    .Servo_type=Servo180,</div>
<div class="line">};</div>
<div class="line"><span class="comment">// 设置好参数后进行初始化并保留返回的指针</span></div>
<div class="line">leftservomoto = ServoInit(&amp;config);</div>
<div class="ttc" id="agroup___t_i_m___channel_html_ga6b1541e4a49d62610899e24bf23f4879"><div class="ttname"><a href="group___t_i_m___channel.html#ga6b1541e4a49d62610899e24bf23f4879">TIM_CHANNEL_1</a></div><div class="ttdeci">#define TIM_CHANNEL_1</div><div class="ttdef"><b>Definition</b> stm32h7xx_hal_tim.h:782</div></div>
<div class="ttc" id="astruct_servo___init___config__s_html"><div class="ttname"><a href="struct_servo___init___config__s.html">Servo_Init_Config_s</a></div><div class="ttdef"><b>Definition</b> servo_motor.h:54</div></div>
<div class="ttc" id="astruct_servo_instance_html"><div class="ttname"><a href="struct_servo_instance.html">ServoInstance</a></div><div class="ttdef"><b>Definition</b> servo_motor.h:70</div></div>
</div><!-- fragment --><p> &gt;要控制一个舵机 我们提供了以下三个接口 </p><div class="fragment"><div class="line"><span class="comment">//自由模式下，写入自由角度数值</span></div>
<div class="line"><span class="keywordtype">void</span> Servo_Motor_FreeAngle_Set(<a class="code hl_struct" href="struct_servo_instance.html">ServoInstance</a> *Servo_Motor, int16_t S_angle);</div>
<div class="line"><span class="comment">//起止模式下，写入起始，终止角度数值(防止反复写入起始和终止角度)</span></div>
<div class="line"><span class="keywordtype">void</span> Servo_Motor_StartSTOP_Angle_Set(<a class="code hl_struct" href="struct_servo_instance.html">ServoInstance</a> *Servo_Motor, int16_t Start_angle, int16_t Final_angle);</div>
<div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment">    Free_Angle_mode, // 任意角度模式</span></div>
<div class="line"><span class="comment">    Start_mode,      // 起始角度模式</span></div>
<div class="line"><span class="comment">    Final_mode,      // 终止角度模式</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"><span class="keywordtype">void</span> Servo_Motor_Type_Select(<a class="code hl_struct" href="struct_servo_instance.html">ServoInstance</a> *Servo_Motor,int16_t mode);</div>
<div class="line"><span class="comment">//比如我们要使用舵机，并更改一个舵机的模式</span></div>
<div class="line"><span class="keywordtype">void</span> ServoTask()</div>
<div class="line">{</div>
<div class="line">    <span class="comment">//更改leftservomoto为Free_Angle_mode模式</span></div>
<div class="line">    Servo_Motor_Type_Select(leftservomoto,Free_Angle_mode);</div>
<div class="line">    <span class="comment">//设置转到0角度</span></div>
<div class="line">    Servo_Motor_FreeAngle_Set(leftservomoto, 0);</div>
<div class="line">    <span class="comment">//调用函数，控制电机</span></div>
<div class="line">    Servo_Motor_Control();</div>
<div class="line">}</div>
</div><!-- fragment --> <hr  />
 <blockquote class="doxtable">
<blockquote class="doxtable">
<blockquote class="doxtable">
<blockquote class="doxtable">
<blockquote class="doxtable">
<blockquote class="doxtable">
<blockquote class="doxtable">
<p>0053f7523b5b581171719f5d4276ee4d31880b98 </p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<div id="page-nav" class="page-nav-panel">
<div id="page-nav-resize-handle"></div>
<div id="page-nav-tree">
<div id="page-nav-contents">
</div><!-- page-nav-contents -->
</div><!-- page-nav-tree -->
</div><!-- page-nav -->
</div><!-- container -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.14.0 </li>
  </ul>
</div>
</body>
</html>
